package com.feemanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.core.page.PageDomain;
import com.common.utils.sql.SqlUtil;
import com.feemanage.domain.BizFeeAlarmSet;
import com.feemanage.domain.BizSystemRemind;
import com.feemanage.mapper.bizFeeAlarmSetMapper;
import com.feemanage.mapper.bizSystemRemindMapper;
import com.feemanage.service.IBizSystemRemindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BizSystemRemindServiceImpl implements IBizSystemRemindService {

    @Autowired
    private bizSystemRemindMapper baseMapper;

    @Autowired
    public bizFeeAlarmSetMapper setMapper;

    private final static String businesskey ="feeExecuteRemind";
    /**
     * 查询列表
     * @param pageDomain
     * @param remind
     * @return
     */
    @Override
    public IPage<BizSystemRemind> selectList(PageDomain pageDomain, BizSystemRemind remind) {
        Page<BizSystemRemind> page = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        QueryWrapper<BizSystemRemind> wrapper = Wrappers.query();
        wrapper.like(StringUtils.isNotBlank(remind.getTitle()), "title", remind.getTitle())
                .like(StringUtils.isNotBlank(remind.getContent()),"content",remind.getContent())
                .like(StringUtils.isNotBlank(remind.getRevicerName()),"revicer_name",remind.getRevicerName());
        if(StringUtils.isNotEmpty(remind.getNoticeWay())){
            List<String> list = Arrays.stream(remind.getNoticeWay().split(",")).collect(Collectors.toList());
            wrapper.in("notice_way",list);
        }
        wrapper.eq("business_key",businesskey);
        String orderByColumn = SqlUtil.escapeOrderBySql(pageDomain.getOrderByColumn());
        boolean isASC = SqlUtil.escapeOrderBySql(pageDomain.getIsAsc()).equalsIgnoreCase("asc");
        if (!StringUtils.isEmpty(orderByColumn))
        {
            wrapper.orderBy(true,isASC,orderByColumn);
        }
        return baseMapper.selectPage(page,wrapper);
    }

    @Override
    public IPage<BizFeeAlarmSet> selectAlarmSetList(PageDomain pageDomain, BizFeeAlarmSet remind) {
        Page<BizFeeAlarmSet> page = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        QueryWrapper<BizFeeAlarmSet> wrapper = Wrappers.query();
        String orderByColumn = SqlUtil.escapeOrderBySql(pageDomain.getOrderByColumn());
        boolean isASC = SqlUtil.escapeOrderBySql(pageDomain.getIsAsc()).equalsIgnoreCase("asc");
        if (!StringUtils.isEmpty(orderByColumn))
        {
            wrapper.orderBy(true,isASC,orderByColumn);
        }
        return setMapper.selectAlarmSetList(page,wrapper);
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @Override
    public BizFeeAlarmSet getAlarmSetInfo(String id){
        return setMapper.selectAlarmSetOne(new QueryWrapper<BizFeeAlarmSet>().eq("s.id",id));
    }


    /**
     * 新增
     * @param feeAlarmSet
     * @return
     */
    @Override
    public Boolean addfeeAlarmSet(BizFeeAlarmSet feeAlarmSet) {
        return setMapper.insert(feeAlarmSet)>0;
    }


    /**
     * 编辑
     * @param feeAlarmSet
     * @return
     */
    @Override
    public Boolean editfeeAlarmSet(BizFeeAlarmSet feeAlarmSet) {
        return setMapper.updateById(feeAlarmSet) >0;
    }



    /**
     * 删除
     * @param ids
     * @param isValid
     * @return
     */
    @Override
    public Boolean delfeeAlarmSet(Collection<String> ids, Boolean isValid){

        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        Boolean flag = setMapper.deleteBatchIds(ids) >0;
        return flag;
    }
}
